﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Reflection;

namespace IRC.IRepairClouds.DataLayer
{
    public static class Entity
    {
        public static bool EqualsKey<T>(T item1, T item2) where T : global::System.Data.Objects.DataClasses.EntityObject
        {
            if (item1 == null && item2 == null)
                return true;
            if (item1 == null || item2 == null)
                return false;

            if (item1.EntityKey == null || item2.EntityKey == null)
                throw new NotSupportedException();

            return item1.EntityKey == item2.EntityKey;
        }

        public static bool EqualsKey<T>(params T[] items) where T : global::System.Data.Objects.DataClasses.EntityObject
        {
            if (items.Length == 0)
                return true;
            if(items.All(i => i == null))
                return true;
            if (items.Any(i => i == null))
                return false;

            if (items.Any(i => i.EntityKey == null))
                throw new NotSupportedException();
            var key = items[0].EntityKey;
            return items.Skip(1).All(i => i.EntityKey == key);
        }
    }
}
